import store from '@/store';

const getSessionItem = (key) => {
  const item: any = sessionStorage.getItem(key);
  if (item instanceof Array || item instanceof Object) {
    if (item instanceof Array) {
      return JSON.parse(sessionStorage.getItem(key)) || [];
    } else {
      return JSON.parse(sessionStorage.getItem(key)) || {};
    }
  } else {
    return item;
  }
};

const setSessionItem = (key, value) => {
  if (value) {
    if (value instanceof Array || value instanceof Object) {
      sessionStorage.setItem(key, JSON.stringify(value));
    } else {
      sessionStorage.setItem(key, value);
    }
  }
};

export const useMapStore = defineStore('map', () => {
  const map3d = ref({}); //map的对象
  const navList = ref(getSessionItem('navList'));
  const activeNav = ref(getSessionItem('activeNav')); //当前选中的nav
  const activeBtm = ref(getSessionItem('activeBtm'));
  const btmList = ref(getSessionItem('btmList'));
  const iscountry = ref();
  const isimg = ref();
  const issw = ref();
  const isDate = ref();
  const isWeather = ref();
  const country = ref(); //当前所在行政区 -值
  const countryText = ref(); // 当前所在行政区名称
  // 数字乡村选择的村庄
  const szxcCountry = ref('4205060050040000000');
  const szxcCountryText = ref('张家口村');
  const isDia = ref(false);
  const activeTab = ref(getSessionItem('activeTab'));
  // 所属村
  const ssc = ref([]);

  const setMap3d = (value) => {
    map3d.value = value;
  };
  const SactiveNav = (value) => {
    activeNav.value = value;
    setSessionItem('activeNav', value);
  };
  const SactiveBtm = (value) => {
    activeBtm.value = value;
    setSessionItem('activeBtm', value);
  };
  const SetBtmList = (value = []) => {
    if (value && value.length > 0) {
      activeBtm.value = value ? value[0].value : '';
      isDia.value = value ? (value[0].isDia ? true : false) : false;
    }
    btmList.value = value;
    setSessionItem('btmList', value);
  };
  const SetDia = (value) => {
    isDia.value = value.isDia ? true : false;
  };
  const SetactiveTab = (value) => {
    activeTab.value = value;
    setSessionItem('activeTab', value);
  };
  const SetNavList = (value) => {
    const list = value || [];

    const item = list.find((item) => item.value === activeNav.value);
    // 当前选项不在列表中，则设置第一个
    if (!item) {
      if (value && value[0]) {
        activeNav.value = value[0].value;
      } else {
        activeNav.value = '';
      }
    }
    navList.value = value;
    setSessionItem('navList', value);
  };
  const SetIscountry = (value) => {
    iscountry.value = value;
  };
  const SetIsimg = (value) => {
    isimg.value = value;
  };
  const SetCountry = (value) => {
    country.value = value;
  };

  const SetCountryText = (value) => {
    countryText.value = value;
  };
  const SetSw = (value) => {
    issw.value = value;
  };
  const SetDate = (value) => {
    isDate.value = value;
  };
  const SetWeather = (value) => {
    isWeather.value = value;
  };
  // 设置所属村
  const SetSsc = (value) => {
    ssc.value = value;
  };
  // 数字乡村村庄设置
  const SetSzxcCountry = (value) => {
    szxcCountry.value = value;
  };
  // 数字乡村村庄名称设置
  const SetSzxcCountryText = (value) => {
    szxcCountryText.value = value;
  };

  return {
    map3d,
    setMap3d,
    activeNav,
    SactiveNav,
    activeBtm,
    SactiveBtm,
    SetBtmList,
    btmList,
    SetNavList,
    navList,
    SetIscountry,
    iscountry,
    SetIsimg,
    isimg,
    SetCountry,
    SetCountryText,
    countryText,
    country,
    issw,
    SetSw,
    SetDate,
    SetWeather,
    isDate,
    isWeather,
    isDia,
    SetDia,
    activeTab,
    SetactiveTab,
    ssc,
    SetSsc,
    szxcCountry,
    SetSzxcCountry,
    szxcCountryText,
    SetSzxcCountryText
  };
});

export default useMapStore;
// 非setup
export function useMapStoreHook() {
  return useMapStore(store);
}
